/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.gl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import src.domain.gl.Glaccnt;
import src.services.gl.GlaccntModel;
import src.util.Constanta;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class GlaccntBean implements Serializable {

    private List<Glaccnt> glaccnts;
    private TreeNode rootNode;
    private List<SelectItem> avblAcctype, avblGL;
    private Glaccnt selectedData, insertedData;
    private boolean editMode = true;
    private TreeNode selectedNode;
    private GlaccntModel helper;

    public GlaccntBean() {
        helper = new GlaccntModel();
        this.glaccnts = helper.findAll(null);
        rootNode = new DefaultTreeNode("root", null);
        generateTreeNode(null,rootNode);
        this.insertedData = null;
        this.selectedData = null;
        this.avblAcctype = Constanta.getAccountType();
        this.avblGL = new GlaccntModel().listAll();
    }

    public boolean isEditMode() {
        return editMode;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
        if (selectedNode!=null) 
            this.selectedData = (Glaccnt) this.selectedNode.getData();
    }
    
    
    public List<Glaccnt> getGlaccnts() {
        return this.glaccnts;
    }
    
    public Glaccnt getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Glaccnt selectedData) {
        this.selectedData = selectedData;
    }
    
    public Glaccnt getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Glaccnt insertedData) {
        this.insertedData = insertedData;
    }

    public List<SelectItem> getAvblAcctype() {
        return avblAcctype;
    }

    public List<SelectItem> getAvblGL() {
        return avblGL;
    }
        
    public String getAccTypeText(short id) {
        String jenis = Constanta.getAccTypeText(id);
        return jenis;
     }

    public TreeNode getRootNode() {
        return rootNode;
    }
    
    
    
    public String selectItemValueOf(Integer id) {
        return (String) Utils.selectItemValueOf(avblGL, id);
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.avblGL = new GlaccntModel().listAll();
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Glaccnt();
    }
     public void openData() {  
        this.avblGL = new GlaccntModel().listAll();
        this.editMode = true;
        this.insertedData = Glaccnt.clone(selectedData);
    }
     
    public void trySave() {
        boolean valid = true;
        if (editMode==true)
            valid = new GlaccntModel().validateParent(insertedData.getParentGlaccntId());
        
        if (valid==true) save();
        else Messager.showError("glUsed");
    } 
    
    private void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
     //   GlaccntModel helper = new GlaccntModel(); 
        if (insertedData.getParentGlaccntId()==-1) insertedData.setParentGlaccntId(null);
        
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.glaccnts.add(insertedData);
            else {
                int i = glaccnts.indexOf(selectedData);
                if (i <= glaccnts.size()) glaccnts.set(i, Glaccnt.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setGlaccntId(null);
        } 
      
        if (success) { 
           // this.glaccnts = helper.findAll(null);
            rootNode = new DefaultTreeNode("root",null);    
            generateTreeNode(null,rootNode);
        }
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;
       // GlaccntModel helper = new GlaccntModel();
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.glaccnts.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
    public void onNodeSelect(NodeSelectEvent event) {
        this.selectedData = (Glaccnt)event.getTreeNode().getData();
    }
    
    public Glaccnt getById(Integer id) {
        return helper.getById(id);
    }
    
    private void generateTreeNode(Integer parentGl,TreeNode parent){
        
        Map<String, Object> filters = new HashMap<>();       
        filters.put("parentGlaccntId", parentGl);
        List<Glaccnt> data = helper.findAll(filters);;
        //this.glaccnts = helper.findAll(filters);
        if (data.isEmpty()){
            return;
        }
        else {
            for (int i=0;i<data.size();i++){
                Glaccnt gl = data.get(i);
                //if (gl.getParentGlaccntId()==null){
                    TreeNode x = new DefaultTreeNode(gl, parent);
                    
                    generateTreeNode(gl.getGlaccntId(),x);
                ////}
            }
            
                      
        }
            
    }
}
